//
//  ticketDetailsView.m
//  TQY
//
//  Created by 甘世清 on 2024/11/10.
//

#import "ticketDetailsView.h"
#import <RMStore/RMStore.h>
#import "IMMessageManager.h"
#import "FiveStarImageViewController.h"
@implementation ticketDetailsView


- (void)SQ_UIViewForSelfView{
    self.backgroundColor = UIColor.clearColor;
    self.layer.cornerRadius = 30;
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"ticketbg");
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    UIImageView * imageView2 = [[UIImageView alloc]init];
    imageView2.image = SQGetImage(@"插画");
    imageView2.contentMode = UIViewContentModeScaleAspectFill;
    [bgImageView addSubview:imageView2];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.height.mas_equalTo(56);
        make.width.mas_equalTo(70);
        make.right.equalTo(bgImageView.mas_right).offset(-24);
    }];
    
    UILabel * topLabel = [[UILabel alloc]init];
    topLabel.text = SQNSLocalString(@"拒绝孤独从现在开始");
    topLabel.textColor = SQColor(@"#333333");
    topLabel.font = FONT_MEDIUM(23);
    [bgImageView addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(40);
        make.top.equalTo(bgImageView).offset(24);
    }];
    
    UIView * bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor =  SQColor(@"#000000");
    [bgImageView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SCREEN_WIDETH-80);
            make.height.mas_equalTo(70);
            make.top.equalTo(bgImageView).offset(70);
            make.centerX.equalTo(bgImageView);
    }];
    
    UILabel * xianshi = [[UILabel alloc]init];
    xianshi.text = SQNSLocalString(@"限时优惠");
    xianshi.font = FONT_MEDIUM(14);
    xianshi.textColor = SQColor(@"#8760FF");
    [bgView addSubview:xianshi];
    [xianshi mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView).offset(-10);
            make.left.equalTo(bgView).offset(25);
    }];
    
    NSString * number = [Manager SQ_stringForKey:@"number"];
    if (![number isNotEmpty]) {
        [Manager SQ_SetObject:@"24" FormKey:@"number"];
        number = @"24";
    }
    int x = [[UserModel share].userid intValue];
    if (x>100) {
        x = 999;
    }
    NSString * numberstring = [NSString stringWithFormat:@"%d",[number intValue]*x];
    UILabel * numberLabel = [[UILabel alloc]init];
    numberLabel.text = [NSString stringWithFormat:@"已有%@位优质女性加入",numberstring];
    numberLabel.font  =FONT_REGULAR(12);
    numberLabel.textColor = SQColor(@"#FFFFFF");
    [bgView addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView).offset(12);
            make.left.equalTo(bgView).offset(25);
    }];
    numberLabel.keywords = numberstring;
    numberLabel.keywordsColor = SQColor(@"#EEAD0E");
    [numberLabel getLableSizeWithMaxWidth:100];
    [Manager SQ_SetObject:[NSString stringWithFormat:@"%d",[number intValue]+1] FormKey:@"number"];
    
    UIView * minView = [[UIView alloc]init];
    minView.layer.cornerRadius = 8;
    minView.clipsToBounds = YES;
    minView.layer.masksToBounds = YES;
    [bgView addSubview:minView];
    [minView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.height.mas_equalTo(60);
            make.right.equalTo(bgView.mas_right).offset(-5);
            make.width.mas_equalTo(70);
    }];
    [minView layoutIfNeeded];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame =  minView.bounds;
    gradient.startPoint = CGPointMake(0, 1);
    gradient.endPoint =CGPointMake(1, 1);
    gradient.colors = [NSArray arrayWithObjects:
    (id)SQColor(@"#A987FF").CGColor,(id)SQColor(@"#FA7BFF").CGColor,nil];
    [minView.layer addSublayer:gradient];
    
    
    [minView addSubview:self.nameLabel];
    [minView addSubview:self.lineLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(minView).insets(UIEdgeInsetsMake(5, 0, 25, 0));
    }];
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(minView).insets(UIEdgeInsetsMake(35, 0, 0, 0));
    }];
    
    NSArray * array = @[@"查看附近近距离优质异性,遇到喜欢的人",@"发布或浏览最新求搭信息,搭子随便撩",@"互动聊天发消息,夜晚不再孤单"];
    for (int x = 0; x< array.count; x++) {
        UIImageView * leftImage = [[UIImageView alloc]init];
        leftImage.image = SQGetImage(@"协议 1");
        [bgImageView addSubview:leftImage];
        [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(bgView).offset(20);
                    make.size.mas_equalTo(13);
                    make.top.equalTo(bgView.mas_bottom).offset(20+x*25);
        }];
        [self layoutIfNeeded];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((x+0.1) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [Manager SQ_addAnimation:leftImage];
        });
        UILabel * label = [[UILabel alloc]init];
        label.text = SQNSLocalString([array objectAtIndex:x]);
        label.textColor = SQColor(@"#FFFFFF");
        label.font = FONT_REGULAR(13);
        [bgImageView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(leftImage);
                    make.left.equalTo(bgImageView).offset(84);
        }];
    }
    
    self.addbtn = [[HeaderView alloc]init];
    self.addbtn.text = SQNSLocalString(@"立即加入");
    self.addbtn.font = FONT_SEMIBOLD(18);
    self.addbtn.colorString = @"#000000";
    self.addbtn.image = @"bg矩形";
    self.addbtn.layer.cornerRadius = 25;
    self.addbtn.layer.masksToBounds = YES;
    [self addSubview: self.addbtn];
    [ self.addbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(SCREEN_WIDETH-80);
            make.height.mas_equalTo(50);
            make.centerX.equalTo(self);
            make.top.equalTo(bgImageView).offset(250);
    }];
    
    UIImageView * imageView3 = [[UIImageView alloc]init];
    imageView3.image = SQGetImage(@"箭头");
    imageView3.contentMode = UIViewContentModeScaleAspectFill;
    [self.addbtn addSubview:imageView3];
    [imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.addbtn);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(22);
        make.right.equalTo(self.addbtn.mas_right).offset(-20);
    }];
     
    [self addSubview:self.fiveBtn];
    [self.fiveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(190);
            make.height.mas_equalTo(50);
            make.centerX.equalTo(self);
            make.top.equalTo(self.addbtn.mas_bottom).offset(15);
    }];
    
    UIImageView * imageView4 = [[UIImageView alloc]init];
    imageView4.image = SQGetImage(@"fivebg");
    imageView4.contentMode = UIViewContentModeScaleAspectFill;
    [self.fiveBtn addSubview:imageView4];
    [imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.fiveBtn);
        make.size.mas_equalTo(16);
        make.left.equalTo(self.fiveBtn).offset(20);
    }];
    
    UIImageView * imageView5 = [[UIImageView alloc]init];
    imageView5.image = SQGetImage(@"fivebg");
    imageView5.contentMode = UIViewContentModeScaleAspectFill;
    [self.fiveBtn addSubview:imageView5];
    [imageView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.fiveBtn);
        make.size.mas_equalTo(16);
        make.right.equalTo(self.fiveBtn.mas_right).offset(-20);
    }];
    
    WS(weakSelf);
    [self.fiveBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_headerButtonClick];
    }];
    [self SQ_Header];
}
-(void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"ticket" forKey:@"key"];
    [HttpTool post:@"config/details" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSString * value = [NSString stringWithFormat:@"%@",[httpModel.data objectForKey:@"value"]];
        if (![value isNotEmpty]) {
            return;
        }
        [self SQ_reloadDataUI:value];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
    }];
}
-(void)SQ_reloadDataUI:(NSString*)value{
    self.ticketDic = [[NSDictionary alloc]initWithDictionary:[value mj_JSONObject]];
    self.nameLabel.text = [NSString stringWithFormat:@"%@%@",[self.ticketDic objectForKey:@"prefix"],[self.ticketDic objectForKey:@"price"]];
    self.nameLabel.keywordsFont = FONT_REGULAR(22);
    self.nameLabel.keywords = [NSString stringWithFormat:@"%@",[self.ticketDic objectForKey:@"price"]];
    [self.nameLabel getLableSizeWithMaxWidth:80];
    self.lineLabel.text = [NSString stringWithFormat:@"%@%@%@",SQNSLocalString(@"原价"),[self.ticketDic objectForKey:@"prefix"],[self.ticketDic objectForKey:@"orgPrice"]];
    self.lineLabel.deletelineColor = UIColor.whiteColor;
    self.lineLabel.deletelineStr = self.lineLabel.text;
    [self.lineLabel getLableSizeWithMaxWidth:80];
    
    NSString * appleId = [NSString stringWithFormat:@"%@",[self.ticketDic objectForKey:@"appleId"]];
    [[RMStore defaultStore] requestProducts:[NSSet setWithArray:@[appleId]] success:^(NSArray *products, NSArray *invalidProductIdentifiers) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
       if([products count] == 0){
           DTLog(@"--------------没有商品------------------");
           return;
       }
   } failure:^(NSError *error) {
       dispatch_async(dispatch_get_main_queue(), ^{
           [YJProgressHUD hide];
       });
   }];
    [self SQ_footer];
}

-(void)SQ_footer{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"100" forKey:@"num"];
    [HttpTool post:@"greet/list" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSArray * array = [NSArray arrayWithArray:httpModel.data];
        [self.peopleArray removeAllObjects];
        [self.peopleArray addObjectsFromArray:array];
        if (self.timer120) {
            [self.timer120 invalidate];
            self.timer120 = nil;
        }
        [self SQ_showMessage];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
    }];
}
-(void)SQ_showMessage{
    [self SQ_120Time];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(SQ_120Time) userInfo:nil repeats:YES];
    self.timer120 = timer;
}
-(void)SQ_120Time{
  NSString * ticket_switch = [Manager SQ_stringForKey:@"ticket_switch"];
    if (![ticket_switch isEqualToString:@"1"]) {
        [_timer120 invalidate];
        _timer120 = nil;
        return;
    }
    if ([UserModel share].isTicket) {
        [_timer120 invalidate];
        _timer120 = nil;
        return;
    }
    if (self.peopleArray.count<=0 ) {
        [self SQ_footer];
    } else {
        NSDictionary * dic = [self.peopleArray lastObject];
        IMMessageModel * mo = [IMMessageModel new];
        mo.toUserName = [dic objectForKey:@"name"];
        mo.toUserPortrait = [dic objectForKey:@"portrait"];
        mo.content = [dic objectForKey:@"content"];
        [IMMessageManager SQ_showMessage:mo];
        [self.peopleArray removeLastObject];
    }
}
-(void)SQ_headerButtonClick{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"五星好评免费试用") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"立刻好评") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/6736890354?action=write-review"]];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"上传截图") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        FiveStarImageViewController * vc = [[FiveStarImageViewController alloc]init];
        [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
    }];
 
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [[Manager SQ_getCurrentViewController] presentViewController:alert animated:YES completion:nil];
}
- (void)dealloc{
    
    
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textColor = UIColor.whiteColor;
        _nameLabel.adjustsFontSizeToFitWidth = YES;
        _nameLabel.font = FONT_REGULAR(10);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}
- (UILabel *)lineLabel{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc]init];
        _lineLabel.textColor = UIColor.whiteColor;
        _lineLabel.adjustsFontSizeToFitWidth = YES;
        _lineLabel.font = FONT_REGULAR(12);
        _lineLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _lineLabel;
}

- (SQCustomButton *)fiveBtn{
    if (!_fiveBtn) {
        _fiveBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _fiveBtn.text = SQNSLocalString(@"五星好评免费试用");
        _fiveBtn.textColor = SQColor(@"#FF9F0A");
        _fiveBtn.font = FONT_REGULAR(14);
    }
    return _fiveBtn;
}
- (NSMutableArray *)peopleArray{
    if (!_peopleArray) {
        _peopleArray = [[NSMutableArray alloc]init];
    }
    return _peopleArray;
}
@end
